package com.sand.controller;

import com.sand.dto.Select.UserSelect;
import com.sand.dto.user.*;
import com.sand.entity.user.User;
import com.sand.result.PageResult;
import com.sand.result.ResponseResult;
import com.sand.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    //登录(完成)
    @PostMapping("/login")
    public ResponseResult<Map<String,Object>> login(@RequestBody(required = false) UserLogin login){

        Map<String,Object> map =  userService.login(login);

        return new ResponseResult<Map<String,Object>>().okResult(map , "登录成功");
    }

    //添加用户(完成)
    @PostMapping("/add")
    public ResponseResult<String> add(@RequestBody AddUserDto user){

        String string = userService.add(user);

        return new ResponseResult<String>().hint(string);
    }

    //手机号和id查询用户(完成)
    @PostMapping("/select")
    private ResponseResult<User> inquire(@RequestBody UserSelect userSelect){
        User user = userService.inquire(userSelect);
        return new ResponseResult<User>().ok(user);
    }

    //根据id查询用户(完成)
    @PostMapping("/getById")
    private ResponseResult<User> getById(@RequestBody Integer userId){
        return new ResponseResult<User>().ok(userService.getById(userId));
    }

    //根据条件查询用户(完成)
    @PostMapping("/list")
    private PageResult<List<User>> getList(@RequestBody UserSelectList list){

        List<User> result = userService.getList(list);

        PageResult<List<User>> pageResult = new PageResult<>();;

        return pageResult.ok(result,result.size());
    }

    //修改当前用户基本信息(完成)
    @PostMapping("/update")
    private ResponseResult<String> UserUpdate(@RequestBody UserUpdate userUpdate){
        String str =  userService.UserUpate(userUpdate);
        return new ResponseResult<String>().hint(str);
    }

    /**
     * 修改电话号码(后期可以采用验证码校验)
     */
    @PostMapping("/UpdatePhone")
    private ResponseResult<String> UserUpdatePhone(@RequestBody UserUpdatePhone updatePhone){
        String str = userService.updatePhone(updatePhone);
        return new ResponseResult<String>().hint(str);
    }

    /**
     * 修改密码
     */
    @PostMapping("/password")
    private ResponseResult<String> UpdatePassword(@RequestBody UserUpdatePassword updatePassword){
        String str = userService.UpdatePassword(updatePassword);
        return new ResponseResult<String>().hint(str);
    }



    //根据多个id返回多个用户基本数据(完成)
    @PostMapping("/ids")
    private ResponseResult<List<User>> ids(@RequestBody Integer[] ids){
        List<User> list =  userService.ids(ids);
        return new ResponseResult<List<User>>().ok(list);
    }


    //修改用户状态(完成)
    @PostMapping("/status")
    private ResponseResult<String> status(@RequestBody Integer id){
        String msg =  userService.status(id);
        return new ResponseResult<String>().hint(msg);
    }

    //获取所有用户数量
    @PostMapping("/getMount")
    private ResponseResult<UserAmount> amount(){
        UserAmount userAmount =  userService.amount();
        return new ResponseResult<UserAmount>().okResult(userAmount);
    }


    //获取验证码
    @PostMapping("/yanzhenma")
    private ResponseResult<String> YanZengMa(@RequestParam(required = false) String yzm){
        String str = userService.YanZengMa(yzm);
        return new ResponseResult<String>().ok(str);
    }


    //删除用户




}
